﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utils
{
    public class Captcha
    {
        #region Private Field
        /// <summary>
        /// 随机种子
        /// </summary>
        private Random objRandom = new Random();
        #endregion

        #region Public Property

        #region 验证码长度
        /// <summary>
        /// 验证码长度
        /// </summary>
        private int length = 4;
        /// <summary>
        /// 验证码长度(默认为4)
        /// </summary>
        public int Length
        {
            get { return this.length; }
            set { this.length = value; }
        }
        #endregion

        #region 验证码字符串
        /// <summary>
        /// 验证码字符串
        /// </summary>
        private string verifyCodeText = null;

        /// <summary>
        /// 验证码字符串
        /// </summary>
        public string VerifyCodeText
        {
            get { return this.verifyCodeText; }
            set { this.verifyCodeText = value; }
        }
        #endregion

        #region 是否加入小写字母
        /// <summary>
        /// 是否加入小写字母
        /// </summary>
        private bool addLowerLetter = true;

        /// <summary>
        /// 是否加入小写字母(不包括o)
        /// </summary>
        public bool AddLowerLetter
        {
            get { return this.addLowerLetter; }
            set { this.addLowerLetter = value; }
        }
        #endregion

        #region 是否加入大写字母
        /// <summary>
        /// 是否加入大写字母
        /// </summary>
        private bool addUpperLetter = true;

        /// <summary>
        /// 是否加入大写字母(不包括O)
        /// </summary>
        public bool AddUpperLetter
        {
            get { return this.addUpperLetter; }
            set { this.addUpperLetter = value; }
        }
        #endregion

        #endregion

        #region Constructor Method
        /// <summary>
        /// 构造方法
        /// </summary>
        public Captcha()
        {
        }
        #endregion

        #region Public Method
        /// <summary>
        /// 得到验证码字符串
        /// </summary>
        public string GetText()
        {
            string text = string.Empty;
            //没有外部输入验证码时随机生成
            StringBuilder objStringBuilder = new StringBuilder();

            //加入数字1-9
            for (int i = 1; i <= 9; i++)
            {
                objStringBuilder.Append(i.ToString());
            }
            //加入大写字母A-Z，不包括O
            if (this.addUpperLetter)
            {
                char temp = ' ';

                for (int i = 0; i < 26; i++)
                {
                    temp = Convert.ToChar(i + 65);

                    //如果生成的字母不是'O'
                    if (!temp.Equals('O'))
                    {
                        objStringBuilder.Append(temp);
                    }
                }
            }
            //加入小写字母a-z，不包括o
            if (this.addLowerLetter)
            {
                char temp = ' ';

                for (int i = 0; i < 26; i++)
                {
                    temp = Convert.ToChar(i + 97);

                    //如果生成的字母不是'o'
                    if (!temp.Equals('o'))
                    {
                        objStringBuilder.Append(temp);
                    }
                }
            }
            //生成验证码字符串
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = objRandom.Next(0, objStringBuilder.Length);

                text += objStringBuilder[index];

                objStringBuilder.Remove(index, 1);
            }
            return text;
        }

        #endregion
    }
}
